//////////////////////////////////////////////////////////////
//////// My custom Keyword Functions                     ////
////////////////////////////////////////////////////////////

package AdminTab;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class KW {
	static WebDriver myD;

	
	public static void openBrowser(String vType){
		//purpose : Open a browser; timeout hardcoded, if want, can send as an input
		//i/p : which browser
		// o/p : N/A
		myD = new FirefoxDriver();
		
		myD.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
	}
	public static void navigateBrowser(String Data){
		//purpose : Navigates a browser; timeout
		//i/p : which browser
		// o/p : N/A
		//myD.get(Data);
		myD.navigate().to(Data);
		
	}
	
	public static void closeBrowser(){
		//purpose : closes a browser
		//i/p : which browser
		// o/p : N/A
		//myD.quit();
		myD.close();
		
	}
	
	public static void quitBrowser(){
		//purpose : closes a browser
		//i/p : which browser
		// o/p : N/A
		//myD.quit();
		myD.quit();
		
	}
	
	public static void linkClick(String Text){
		//purpose : Click on a link specified
				//i/p :link text
				// o/p : N/A
		myD.findElement(By.linkText(Text)).click();
		
	}
	
	public static void enterText(String xPath, String Data) {
		// Purpose: Enter text into a edit field
		//i/p: xPath, Data
		//o/p:  Nil
		
		
		myD.findElement(By.xpath(xPath)).clear();//some applications already have a default values in edit field, so this line is to clear the default path
		myD.findElement(By.xpath(xPath)).sendKeys(Data);
		
	}
	
	public static String verifyTitle(String vTitle) {
		// Purpose: verify title of the page
		//i/p: Title
		//o/p:  Nil		
		
				if (vTitle.equalsIgnoreCase(myD.getTitle())){  //getTitle is a method to get title, no xpath required
					
					return "pass"; 
					
				} else {
					return "Fail";
				}
	}
	
	public static String verifyText(String xPath, String vText) {
		// Purpose: Enter text into a edit field
		//i/p: xPath, Data
		//o/p:  Nil		
		
				if (vText.equalsIgnoreCase(myD.findElement(By.xpath(xPath)).getText())){  //returns whatever comes here
					
					return "pass"; 
					
				} else {
					return "Fail";
				}
	}
			
	public static void clickElement(String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
					// Purpose: Click any element on the AUT
					//i/p: driver, xPath
					//o/p:  NIl
					
					myD.findElement(By.xpath(xPath)).click(); // sometimes click may not work, so I write a code to send enter
					
					//driver.findElement(By.xpath(xPath)).sendKeys("\n");  //\n is enter key
					
				}
				
	public static void checkBox(String xPath) {  //this has to be checked.
		// Purpose: checks a checkbox
		//i/p: driver, xPath
		//o/p:  NIl
				
	//Is it checked?
		if(myD.findElement(By.xpath(xPath)).isSelected()){
			//then click
			
			System.out.println("checked");
		} else {
		myD.findElement(By.xpath(xPath)).click();
		System.out.println("Now checked");

		}		
	}
	
	public static String defaultvaluelist(String xPath, String Data){
	//Purpose : Verifies the default value
		
		String Appvalue = myD.findElement(By.xpath(xPath)).getText();
	
		if (Data.equalsIgnoreCase(Appvalue)){
			return "pass";
		} else {

		 //return "Fail: The Default value is incorrect";
				return "Fail: the Default value does not match on the console.";
				
		}
		
	
	}
			
	public static String readText(String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
	// Purpose: read text from any field.
	//i/p: driver, xPath
	//o/p:  Data
	
	return myD.findElement(By.xpath(xPath)).getText(); //returns whatever comes here
	}

	

	public static void selectList(String xPath, String Data) { 
	// Purpose: Select from a drop down list 
	//i/p: driver, xPath, Data
	//o/p:  NIl

	
		Select myList = new Select(myD.findElement(By.xpath(xPath)));//creating memory in myList
		myList.selectByVisibleText(Data);
		myList = null;  //clears the memory in my list
	
	}

	public static void waitTime(long lTime) throws Exception{
	// Purpose: it will wait for a certain amount of time.
				//i/p: time in milli seconds
				//o/p:  NIl
				
	Thread.sleep(lTime);
	
	}
	public static void alertBoxaccept(FirefoxDriver driver) {
	Alert myAlert = driver.switchTo().alert();
	myAlert.accept();
	}
	
	public static void alertBoxdismiss(FirefoxDriver driver) {
	Alert myAlert = driver.switchTo().alert();
	myAlert.dismiss();
	}

	 public static void list_select_value(FirefoxDriver driver, String xpath, String Data){
		 //Purpose : Gives the total elements in the drop down list
		 
		  WebElement myV = driver.findElement(By.xpath(xpath));
		 java.util.List<WebElement> myL =  myV.findElements(By.tagName("option")) ; //List is an array . when I use tag name, I can say the tag name of that elemenst are option
	  	//The above line will list all the elements
		 System.out.println("size of items in drop down is" + myL.size());
		 for(int i=0; i<myL.size(); i++){
			 System.out.println("Value in the item" + i + "is" + myL.get(i).getText());
			 if (myL.get(i).getText().equalsIgnoreCase(Data)){
				 myL.get(i).click();
			 }
		 }
		 
	 }
}
